/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.domain;

import org.synyx.hades.domain.AbstractPersistable;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Entity
@NamedQueries({
	@NamedQuery(name = "Annotation.findByAll", query = "from Annotation o"),
	@NamedQuery(name = "Annotation.findByRevision", query = "from Annotation o join o.revisions r where r = ?")})
public class Annotation extends AbstractPersistable<Long> {
	private String headline = "";
	@Column(length=1024)
	private String text = "";

	@ManyToMany
	private List<Revision> revisions = new ArrayList<Revision>();
	
	public String getHeadline() {
		return headline;
	}

	public void setHeadline(String headline) {
		this.headline = headline;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setRevisions(List<Revision> revisions) {
		this.revisions = revisions;
	}

	public List<Revision> getRevisions() {
		return revisions;
	}

	@Override
	public String toString() {
		return String.format(
				"Annotation [headline=%s, text=%s, getId()=%s]",
				headline, text, getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((headline == null) ? 0 : headline.hashCode());
		result = prime * result
				+ ((revisions == null) ? 0 : revisions.hashCode());
		result = prime * result + ((text == null) ? 0 : text.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (!(obj instanceof Annotation)) {
			return false;
		}
		Annotation other = (Annotation) obj;
		if (headline == null) {
			if (other.headline != null) {
				return false;
			}
		} else if (!headline.equals(other.headline)) {
			return false;
		}
		if (revisions == null) {
			if (other.revisions != null) {
				return false;
			}
		} else if (!revisions.equals(other.revisions)) {
			return false;
		}
		if (text == null) {
			if (other.text != null) {
				return false;
			}
		} else if (!text.equals(other.text)) {
			return false;
		}
		return true;
	}

}
